cmake_minimum_required(VERSION 3.5)

project(OmniCpp VERSION 1.0.0 LANGUAGES CXX C)

# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_INSTALL_PREFIX "/" CACHE PATH "Installation prefix" FORCE)

# 添加编译选项
option(BUILD_CORE "Build core library" ON)
option(BUILD_EXAMPLES "Build example programs" ON)
option(BUILD_TESTS "Build test programs" ON)
option(BUILD_GTEST "Build gtest library" ON)

# 构建OmniCpp库
if(BUILD_CORE)
    file(GLOB_RECURSE SOURCES 
        "${CMAKE_CURRENT_SOURCE_DIR}/log/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/thread/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/sync/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/memery/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/memery/tlsf/*.c"
        "${CMAKE_CURRENT_SOURCE_DIR}/filesystem/*.cpp"
    )

    # 根据构建类型创建静态库或动态库
    if(BUILD_SHARED_LIBS)
        add_library(OmniCpp SHARED ${SOURCES})
        set_target_properties(OmniCpp PROPERTIES
            DEFINE_SYMBOL OMNICPP_EXPORTS
        )
    else()
        add_library(OmniCpp STATIC ${SOURCES})
    endif()

    # 设置库文件输出路径
    set_target_properties(OmniCpp PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    )

    # 设置头文件目录
    target_include_directories(OmniCpp PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    )

    # 设置库属性
    set_target_properties(OmniCpp PROPERTIES
        OUTPUT_NAME "OmniCpp"
        CLEAN_DIRECT_OUTPUT 1
        VERSION ${PROJECT_VERSION}
        SOVERSION ${PROJECT_VERSION_MAJOR}
    )

    # 创建导出集
    install(TARGETS OmniCpp
        EXPORT OmniCppTargets
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        RUNTIME DESTINATION bin
        INCLUDES DESTINATION include
    )

    # 安装头文件
    install(DIRECTORY 
    ${CMAKE_CURRENT_SOURCE_DIR}/containers  
    ${CMAKE_CURRENT_SOURCE_DIR}/filesystem 
    ${CMAKE_CURRENT_SOURCE_DIR}/log 
    ${CMAKE_CURRENT_SOURCE_DIR}/memery 
    ${CMAKE_CURRENT_SOURCE_DIR}/sync 
    ${CMAKE_CURRENT_SOURCE_DIR}/thread 
        DESTINATION include/OmniCpp
        FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
        PATTERN "tlsf" EXCLUDE
    )

    # 安装导出集，用于其他项目通过find_package()查找和使用该库
    install(EXPORT OmniCppTargets
        FILE OmniCppConfig.cmake
        NAMESPACE OmniCpp::
        DESTINATION lib/cmake/OmniCpp
    )
endif()

# 构建gtest库
if(BUILD_GTEST)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/gtest/)
endif()
# 编译单元测试
if(BUILD_TESTS)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test/)
endif()

# 编译使用示例
if(BUILD_EXAMPLES AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/example)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/example/)
endif()